//
//  MyRechargeRecordTableViewController.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/19.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "MyRechargeRecordTableViewController.h"
#import "Config.h"
#import "UIScrollView+VORefresh.h"
#import "QYLoadingView.h"
@interface MyRechargeRecordTableViewController()
{
    NSMutableArray *dataArray;
    BOOL isHasMore;
    QYLoadingView *loadingView;
}
@end
@implementation MyRechargeRecordTableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    [MobClick event:@"2014"];
    [self setBarBackgroundImage:@"navBackground"];
    [self setTitleForNavigation:@"充值记录"];
    [self setLeftBackButtonForNavigation];
     //导航控制器不透明
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.tableView addTopRefreshWithTarget:self action:@selector(getNetNewDataArray)];
    [self.tableView addBottomRefreshWithTarget:self action:@selector(getMoreDataArray)];
    dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getRechargeRecordPage:1]];
    [self.tableView reloadData];
    [self getNetNewDataArray];
}
-(void)getNetNewDataArray{
    [self.navigationController setSGProgressPercentage:80 andTintColor:YellowColor];
    [[ZMYNetWorkAPI default]getRechargeRecordPage:1 Response:^(NSDictionary *resultDic) {
        [self.navigationController setSGProgressPercentage:100  andTintColor:YellowColor];
        dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getRechargeRecordPage:1]];
        [self.tableView reloadData];
        NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
        if (dataArray.count<count) {
            isHasMore = YES;
        }else{
            isHasMore = NO;
            [self.tableView removeBottomRefresh];
        }
        [self.tableView.topRefresh endRefreshing];
    } error:^(NSString *errorStr) {
        [self.tableView.topRefresh endRefreshing];
        [self.navigationController cancelSGProgress];
    }];
}
-(void)getMoreDataArray{
    if (isHasMore) {
        [[ZMYNetWorkAPI default]getRechargeRecordPage:dataArray.count/10+1 Response:^(NSDictionary *resultDic) {
            [self.tableView.bottomRefresh endRefreshing];
            [self.navigationController setSGProgressPercentage:100];
            [dataArray addObjectsFromArray:[[ZMYLKDB getLKDBHelper] getRechargeRecordPage:dataArray.count/10+1]];
            [self.tableView reloadData];
            NSInteger count = [[resultDic objectForKey:@"count"] integerValue];
            if (dataArray.count<count) {
                isHasMore = YES;
            }else{
                isHasMore = NO;
                [self.tableView removeBottomRefresh];
            }
        } error:^(NSString *errorStr) {
            [self.tableView.bottomRefresh endRefreshing];
            [AppDelegate showHUDAndHide:errorStr view:self.view];
        }];
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (dataArray.count<1) {
        self.defaultImageView.hidden = NO;
    } else {
        self.defaultImageView.hidden = YES;
    }
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyRechargeRecordTableViewCell" forIndexPath:indexPath];
    RechargeRecord *recharge = dataArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"充值%@阅读币(%@元)",recharge.coin_num,recharge.money];
    cell.textLabel.textColor = RGB_COLOR(50, 50, 50);
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.text = [NSString dateTimeIntervalToDateStr:@"yyyy-MM-dd hh:mm:ss" TimeInterval:recharge.time];
    cell.detailTextLabel.textColor = RGB_COLOR(160, 160, 160);
//    cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
    if (recharge.isUpdateFinesh) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = nil;
    } else {
        UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        buyButton.frame = CGRectMake(0, 0, 40, 30);
        [buyButton setTitle:@"恢复" forState:UIControlStateNormal];
        [buyButton setTitleColor:RGB_COLOR(116, 178, 77) forState:UIControlStateNormal];
        buyButton.titleLabel.font = [UIFont systemFontOfSize:14];
        buyButton.tag = indexPath.row;
        [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = buyButton;
    }
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, -41);
    return cell;
}
- (void)buyButtonTapped:(id)sender {
    
    UIButton *buyButton = (UIButton *)sender;
    RechargeRecord *recharge = dataArray[buyButton.tag];
    if (!loadingView) {
        loadingView = [[[NSBundle mainBundle]loadNibNamed:@"QYLoadingView" owner:self options:nil] lastObject];
    }
    [loadingView showView];
    //向服务器发生票据
    [[ZMYNetWorkAPI default]updateYuebi:recharge Response:^(NSDictionary *resultDic) {
        [loadingView closeView];
        [AppDelegate showHUDAndHide:@"恭喜您恢复成功，金额已经按时到账！" view:[AppDelegate delegate].window];
        dataArray = [NSMutableArray arrayWithArray:[[ZMYLKDB getLKDBHelper] getRechargeRecordPage:1]];
        [self.tableView reloadData];
    } error:^(NSString *errorStr) {
        [loadingView closeView];
        [AppDelegate showAlert:@"错误" message:@"服务器验证失败,请重试"];
    }];
    
}
@end
